

if( BlackmarketMenu ) then
	BlackmarketMenu:Remove();
end
BlackmarketMenu = nil;

BlackmarketIconGrid = { }
BlackmarketW = 0;
BlackmarketH = 0;
 
function RemoveAllBlackmarketIcons()

	for k, v in pairs( BlackmarketIconGrid ) do
	
		for n, m in pairs( v ) do
		
			if( BlackmarketIconGrid[k][n]:IsValid() ) then
				BlackmarketIconGrid[k][n]:Remove();
				BlackmarketIconGrid[k][n] = nil;
			end
			
		end
	
	end
	
	BlackmarketIconGrid = nil;
	BlackmarketIconGrid = { }
	
	ChosenBlackmarketItem = nil;

end

function RemoveBlackmarketButtons()

	if( not BlackmarketMenu ) then return; end

	if( BlackmarketMenu.TakeButton ) then
		BlackmarketMenu.TakeButton:Remove();
		BlackmarketMenu.TakeButton = nil;
	end

end

function ShowBlackmarketButtons()

	local function take()
	
		local canafford = false;
		
		if( ClientVars["Money"] - TS.ItemsData[ChosenBlackmarketItem.id].Price > 0 ) then
			
			RunConsoleCommand( "eng_buybmitem", ChosenBlackmarketItem.id, ChosenBlackmarketItem.x, ChosenBlackmarketItem.y );
			
			canafford = true;
			
		end
		
		if( not canafford ) then
		
			CreateOkPanel( "Cannot afford", "You do not have enough credits for this purchase" );
		
		end
	
	end

	BlackmarketMenu.TakeButton = BlackmarketMenu:AddButton( "Purchase", 10, 10, take );

end

function CreateBlackmarketMenu( msg )

	if( BlackmarketMenu ) then
		BlackmarketMenu:Remove();
		BlackmarketMenu = nil;
	end
	
	RemoveAllBlackmarketIcons();
	RemoveBlackmarketButtons();
	
	local title = "Blackmarket"
	StorageW = 14
	StorageH = 8

	BlackmarketMenu = CreateBPanel( title, 10, 10, 720, 510 );
	BlackmarketMenu:SetBodyColor( Color( 20, 20, 20, 100 ) );
	
	BlackmarketMenu.OnClose = function()
	
		RunConsoleCommand( "eng_closebm", "" );
	
	end

	BlackmarketMenu.CanPutInto = false;
	
	BlackmarketMenu.DescriptionPanel = CreateBPanel( nil, 150, 0, 560, 70 );
	BlackmarketMenu.DescriptionPanel:SetParent( BlackmarketMenu );
	BlackmarketMenu.DescriptionPanel:SetBodyColor( Color(110, 125, 118, 170 ) );
	
	BlackmarketMenu.ContentPanel = CreateBPanel( nil, 10, 80, 700, 400 );	
	BlackmarketMenu.ContentPanel:SetParent( BlackmarketMenu );
	BlackmarketMenu.ContentPanel:SetBodyColor( Color( 20, 20, 20, 100 ) );
	BlackmarketMenu.ContentPanel:EnableScrolling( true );
	
	BlackmarketMenu.ContentPanel.PaintHook = function()
	
		local row = StorageH - 1;
		local col = StorageW - 1;
		
		local black = false;
		local newblack = true;
		
		for y = 0, row do
		
			for x = 0, col do
			
				if( black ) then
					draw.RoundedBox( 0, x * GRIDWIDTH, y * GRIDHEIGHT, GRIDWIDTH, GRIDHEIGHT, Color( 0, 0, 0, 200 ) );
				end
				
				black = !black;
				
			end
			
			black = newblack;
			newblack = !newblack;
		
		end
	
		if( ChosenBlackmarketItem and ChosenBlackmarketItem.x > -1 ) then
		
			draw.RoundedBox( 0, ChosenBlackmarketItem.x * GRIDWIDTH, ChosenBlackmarketItem.y * GRIDHEIGHT, GRIDWIDTH * TS.ItemsData[ChosenBlackmarketItem.id].Width, GRIDHEIGHT * TS.ItemsData[ChosenBlackmarketItem.id].Height, Color( 0, 0, 128, 128 ) );
		
		end
	
	end
	
	local function ws()
	
			LocalPlayer():ConCommand( "eng_openws" );
			LocalPlayer():ConCommand( "eng_closebm" );

	end
	
	local function ln()
	
		LocalPlayer():ConCommand( "eng_olm" );
		
	end	
	BlackmarketMenu.LoanButton = BlackmarketMenu:AddButton( "Loans", 10, 41, ln );
	
	BlackmarketMenu.WSButton = BlackmarketMenu:AddButton( "Wholesale", 10, 60, ws );
	
	gui.EnableScreenClicker( true );
end
usermessage.Hook( "CBMM", CreateBlackmarketMenu );

function InsertBlackmarketItem( msg )

	if( not BlackmarketMenu ) then return; end

	local item = msg:ReadString();
	local x = msg:ReadShort();
	local y = msg:ReadShort();

	local data = TS.ItemsData[item];
	
	if( not data ) then timer.Simple( 1, InsertBlackmarketItem, msg ); return; end
	
	local icon = vgui.Create( "DModelPanel", BlackmarketMenu.ContentPanel );
	icon:SetModel( data.Model );
	icon:SetPos( x * GRIDWIDTH, y * GRIDHEIGHT );
	icon:SetSize( data.Width * GRIDWIDTH, data.Height * GRIDHEIGHT );
	icon:SetCamPos( data.CamPos );
	icon:SetLookAt( data.LookAt );
	icon:SetFOV( data.Fov );
	
	icon.LayoutEntity = function( self )
	    

 	end
 	
 	icon.OnMousePressed = function()
 	
 		ChosenBlackmarketItem = { id = item, x = x, y = y };
 		ShowBlackmarketButtons();
		ShowBlackmarketDescription( item );
 	
 	end
 	
 	if( BlackmarketIconGrid[x] and BlackmarketIconGrid[x][y] ) then

 		if( BlackmarketIconGrid[x][y]:IsValid() ) then
 			BlackmarketIconGrid[x][y]:Remove();
 		end
 		
 		BlackmarketIconGrid[x][y] = nil;
 		
 	end
 	
 	if( not BlackmarketIconGrid[x] ) then
 		BlackmarketIconGrid[x] = { }
 	end
 	
 	BlackmarketIconGrid[x][y] = icon;


end
usermessage.Hook( "IBMI", InsertBlackmarketItem );

function RemoveBlackmarketItem( msg )

	local x = msg:ReadShort();
	local y = msg:ReadShort();

	if( ChosenBlackmarketItem and
		ChosenBlackmarketItem.x == x and
		ChosenBlackmarketItem.y == y ) then
		
		ChosenBlackmarketItem = nil;
		RemoveBlackmarketButtons();
		
	end
	
	if( BlackmarketMenu.DescriptionPanel.Icon ) then
		BlackmarketMenu.DescriptionPanel.Icon:Remove();
		BlackmarketMenu.DescriptionPanel.Icon = nil;
	end

	if( BlackmarketIconGrid[x] and BlackmarketIconGrid[x][y] ) then
	
		BlackmarketIconGrid[x][y]:Remove();
		BlackmarketIconGrid[x][y] = nil;
		
	end
	

end
usermessage.Hook( "RBMI", RemoveBlackmarketItem );

function ShowBlackmarketDescription( item )

	if( BlackmarketMenu.DescriptionPanel.Icon ) then
		BlackmarketMenu.DescriptionPanel.Icon:Remove();
		BlackmarketMenu.DescriptionPanel.Icon = nil;
	end
	
	local data = TS.ItemsData[item];
	
	BlackmarketMenu.DescriptionPanel.Icon = vgui.Create( "DModelPanel", BlackmarketMenu.DescriptionPanel );
	BlackmarketMenu.DescriptionPanel.Icon:SetModel( data.ContainsModel or data.Model );
	BlackmarketMenu.DescriptionPanel.Icon:SetPos( 2, 2 );
	BlackmarketMenu.DescriptionPanel.Icon:SetSize( math.Clamp( data.Width * GRIDWIDTH * 2, 0, BlackmarketMenu.DescriptionPanel:GetWide() ), math.Clamp( data.Height * GRIDHEIGHT * 2, 0, BlackmarketMenu.DescriptionPanel:GetTall() ) );
	BlackmarketMenu.DescriptionPanel.Icon:SetCamPos( data.CamPos );
	BlackmarketMenu.DescriptionPanel.Icon:SetLookAt( data.LookAt );
	BlackmarketMenu.DescriptionPanel.Icon:SetFOV( data.Fov );
	
	BlackmarketMenu.DescriptionPanel.Text = FormatLine( data.Desc, "NewChatFont", BlackmarketMenu.DescriptionPanel:GetWide() - math.Clamp( data.Width * GRIDWIDTH * 2, 0, BlackmarketMenu.DescriptionPanel:GetWide() ) );

	BlackmarketMenu.DescriptionPanel.PaintHook = function()
	
		draw.DrawText( data.Name, "NewChatFont", BlackmarketMenu.DescriptionPanel:GetWide() - 5, 2, Color( 255, 255, 255, 255 ), 2 );
		draw.DrawText( data.Price .. " credits", "NewChatFont", BlackmarketMenu.DescriptionPanel:GetWide() - 5, 18, Color( 255, 255, 255, 255 ), 2 );
		draw.DrawText( BlackmarketMenu.DescriptionPanel.Text, "NewChatFont", BlackmarketMenu.DescriptionPanel:GetWide() - 2, 34, Color( 255, 255, 255, 255 ), 2 );
		
	end
	
	BlackmarketMenu.DescriptionPanel.Icon.LayoutEntity = function( self ) 
	
	end
	
end

function RemoveBlackmarketMenu()
	
	if( BlackmarketMenu and BlackmarketMenu.TitleBar ) then
	
		BlackmarketMenu.TitleBar:Remove();
		BlackmarketMenu:Remove();

	end

end
usermessage.Hook( "RBMM", RemoveBlackmarketMenu );



if( WholesaleMenu ) then
	WholesaleMenu:Remove();
end
WholesaleMenu = nil;

WholesaleIconGrid = { }
WholesaleW = 0;
WholesaleH = 0;
 
function RemoveAllWholesaleIcons()

	for k, v in pairs( WholesaleIconGrid ) do
	
		for n, m in pairs( v ) do
		
			if( WholesaleIconGrid[k][n]:IsValid() ) then
				WholesaleIconGrid[k][n]:Remove();
				WholesaleIconGrid[k][n] = nil;
			end
			
		end
	
	end
	
	WholesaleIconGrid = nil;
	WholesaleIconGrid = { }
	
	ChosenWholesaleItem = nil;

end

function RemoveWholesaleButtons()

	if( not WholesaleMenu ) then return; end

	if( WholesaleMenu.TakeButton ) then
		WholesaleMenu.TakeButton:Remove();
		WholesaleMenu.TakeButton = nil;
	end

end

function ShowWholesaleButtons()

	local function take()
	
		local canafford = false;
		
		if( ClientVars["Money"] - TS.ItemsData[ChosenWholesaleItem.id].Price > 0 ) then
			
			RunConsoleCommand( "eng_buywholesaleitem", ChosenWholesaleItem.id, ChosenWholesaleItem.x, ChosenWholesaleItem.y );
			
			canafford = true;
			
		end
		
		if( not canafford ) then
		
			CreateOkPanel( "Cannot afford", "You do not have enough credits for this purchase" );
		
		end
	
	end

	WholesaleMenu.TakeButton = WholesaleMenu:AddButton( "Purchase", 55, 10, take );

end

function CreateWholesaleMenu( msg )

	if( WholesaleMenu ) then
		WholesaleMenu:Remove();
		WholesaleMenu = nil;
	end
	
	RemoveAllWholesaleIcons();
	RemoveWholesaleButtons();
	
	local title = "Wholesale Purchase"
	StorageW = 14
	StorageH = 8

	WholesaleMenu = CreateBPanel( title, 10, 10, 720, 510 );
	WholesaleMenu:SetBodyColor( Color( 20, 20, 20, 100 ) );
	
	WholesaleMenu.OnClose = function()
	
		RunConsoleCommand( "eng_closews", "" );
	
	end

	WholesaleMenu.CanPutInto = false;
	
	WholesaleMenu.DescriptionPanel = CreateBPanel( nil, 150, 0, 560, 70 );
	WholesaleMenu.DescriptionPanel:SetParent( WholesaleMenu );
	WholesaleMenu.DescriptionPanel:SetBodyColor( Color( 20, 20, 20, 100 ) );
	
	WholesaleMenu.ContentPanel = CreateBPanel( nil, 10, 80, 700, 400 );	
	WholesaleMenu.ContentPanel:SetParent( WholesaleMenu );
	WholesaleMenu.ContentPanel:SetBodyColor( Color( 20, 20, 20, 100 ) );
	WholesaleMenu.ContentPanel:EnableScrolling( true );
	
	WholesaleMenu.ContentPanel.PaintHook = function()
	
		local row = StorageH - 1;
		local col = StorageW - 1;
		
		local black = false;
		local newblack = true;
		
		for y = 0, row do
		
			for x = 0, col do
			
				if( black ) then
					draw.RoundedBox( 0, x * GRIDWIDTH, y * GRIDHEIGHT, GRIDWIDTH, GRIDHEIGHT, Color( 0, 0, 0, 200 ) );
				end
				
				black = !black;
				
			end
			
			black = newblack;
			newblack = !newblack;
		
		end
	
		if( ChosenWholesaleItem and ChosenWholesaleItem.x > -1 ) then
		
			draw.RoundedBox( 0, ChosenWholesaleItem.x * GRIDWIDTH, ChosenWholesaleItem.y * GRIDHEIGHT, GRIDWIDTH * TS.ItemsData[ChosenWholesaleItem.id].Width, GRIDHEIGHT * TS.ItemsData[ChosenWholesaleItem.id].Height, Color( 0, 0, 128, 128 ) );
		
		end
	
	end
	
	local function bm()
	
			LocalPlayer():ConCommand( "eng_openbm" );
			LocalPlayer():ConCommand( "eng_closews" );

	end

	local function cb()
	
			LocalPlayer():ConCommand( "eng_opencb" );
			LocalPlayer():ConCommand( "eng_closews" );

	end
	
	local function ln()
	
		LocalPlayer():ConCommand( "eng_olm" );
		
	end	
	WholesaleMenu.LoanButton = WholesaleMenu:AddButton( "Loans", 10, 10, ln );
	
	--if( LocalPlayer():HasPlayerFlag( "B" ) ) then
	
	WholesaleMenu.BMButton = WholesaleMenu:AddButton( "Weaponry", 10, 63, bm );

	WholesaleMenu.CBButton = WholesaleMenu:AddButton( "Contraband", 10, 41, cb );
	
	
	--end
	
	gui.EnableScreenClicker( true );
end
usermessage.Hook( "CWM", CreateWholesaleMenu );

function InsertWholesaleItem( msg )

	if( not WholesaleMenu ) then return; end

	local item = msg:ReadString();
	local x = msg:ReadShort();
	local y = msg:ReadShort();

	local data = TS.ItemsData[item];
	
	if( not data ) then timer.Simple( 1, InsertWholesaleItem, msg ); return; end
	
	local icon = vgui.Create( "DModelPanel", WholesaleMenu.ContentPanel );
	icon:SetModel( data.Model );
	icon:SetPos( x * GRIDWIDTH, y * GRIDHEIGHT );
	icon:SetSize( data.Width * GRIDWIDTH, data.Height * GRIDHEIGHT );
	icon:SetCamPos( data.CamPos );
	icon:SetLookAt( data.LookAt );
	icon:SetFOV( data.Fov );
	
	icon.LayoutEntity = function( self )
	    

 	end
 	
 	icon.OnMousePressed = function()
 	
 		ChosenWholesaleItem = { id = item, x = x, y = y };
 		ShowWholesaleButtons();
		ShowWholesaleDescription( item );
 	
 	end
 	
 	if( WholesaleIconGrid[x] and WholesaleIconGrid[x][y] ) then

 		if( WholesaleIconGrid[x][y]:IsValid() ) then
 			WholesaleIconGrid[x][y]:Remove();
 		end
 		
 		WholesaleIconGrid[x][y] = nil;
 		
 	end
 	
 	if( not WholesaleIconGrid[x] ) then
 		WholesaleIconGrid[x] = { }
 	end
 	
 	WholesaleIconGrid[x][y] = icon;


end
usermessage.Hook( "IWI", InsertWholesaleItem );

function RemoveWholesaleItem( msg )

	local x = msg:ReadShort();
	local y = msg:ReadShort();

	if( ChosenWholesaleItem and
		ChosenWholesaleItem.x == x and
		ChosenWholesaleItem.y == y ) then
		
		ChosenWholesaleItem = nil;
		RemoveWholesaleButtons();
		
	end
	
	if( WholesaleMenu.DescriptionPanel.Icon ) then
		WholesaleMenu.DescriptionPanel.Icon:Remove();
		WholesaleMenu.DescriptionPanel.Icon = nil;
	end

	if( WholesaleIconGrid[x] and WholesaleIconGrid[x][y] ) then
	
		WholesaleIconGrid[x][y]:Remove();
		WholesaleIconGrid[x][y] = nil;
		
	end
	

end
usermessage.Hook( "RWI", RemoveWholesaleItem );

function ShowWholesaleDescription( item )

	if( WholesaleMenu.DescriptionPanel.Icon ) then
		WholesaleMenu.DescriptionPanel.Icon:Remove();
		WholesaleMenu.DescriptionPanel.Icon = nil;
	end
	
	local data = TS.ItemsData[item];
	
	WholesaleMenu.DescriptionPanel.Icon = vgui.Create( "DModelPanel", WholesaleMenu.DescriptionPanel );
	WholesaleMenu.DescriptionPanel.Icon:SetModel( data.ContainsModel or data.Model );
	WholesaleMenu.DescriptionPanel.Icon:SetPos( 2, 2 );
	WholesaleMenu.DescriptionPanel.Icon:SetSize( math.Clamp( data.Width * GRIDWIDTH * 2, 0, WholesaleMenu.DescriptionPanel:GetWide() ), math.Clamp( data.Height * GRIDHEIGHT * 2, 0, WholesaleMenu.DescriptionPanel:GetTall() ) );
	WholesaleMenu.DescriptionPanel.Icon:SetCamPos( data.CamPos );
	WholesaleMenu.DescriptionPanel.Icon:SetLookAt( data.LookAt );
	WholesaleMenu.DescriptionPanel.Icon:SetFOV( data.Fov );
	
	WholesaleMenu.DescriptionPanel.Text = FormatLine( data.Desc, "NewChatFont", WholesaleMenu.DescriptionPanel:GetWide() - math.Clamp( data.Width * GRIDWIDTH * 2, 0, WholesaleMenu.DescriptionPanel:GetWide() ) );

	WholesaleMenu.DescriptionPanel.PaintHook = function()
	
		draw.DrawText( data.Name, "NewChatFont", WholesaleMenu.DescriptionPanel:GetWide() - 5, 2, Color( 255, 255, 255, 255 ), 2 );
		draw.DrawText( data.Price .. " credits", "NewChatFont", WholesaleMenu.DescriptionPanel:GetWide() - 5, 18, Color( 255, 255, 255, 255 ), 2 );
		draw.DrawText( WholesaleMenu.DescriptionPanel.Text, "NewChatFont", WholesaleMenu.DescriptionPanel:GetWide() - 2, 34, Color( 255, 255, 255, 255 ), 2 );
		
	end
	
	WholesaleMenu.DescriptionPanel.Icon.LayoutEntity = function( self ) 
	
	end
	
end

function RemoveWholesaleMenu()
	
	if( WholesaleMenu and WholesaleMenu.TitleBar ) then
	
		WholesaleMenu.TitleBar:Remove();
		WholesaleMenu:Remove();

	end

end
usermessage.Hook( "RWM", RemoveWholesaleMenu );

local function NoBM( msg )

	CreateOkPanel( "Nope.", "You do not have access to this" );

end
usermessage.Hook( "NOBM", NoBM );

local function NoCB( msg )

	CreateOkPanel( "Nope.", "You do not have access to this" );

end
usermessage.Hook( "NOCB", NoCB );



if( ContrabandMenu ) then
	ContrabandMenu:Remove();
end
ContrabandMenu = nil;

ContrabandIconGrid = { }
ContrabandW = 0;
ContrabandH = 0;
 
function RemoveAllContrabandIcons()

	for k, v in pairs( ContrabandIconGrid ) do
	
		for n, m in pairs( v ) do
		
			if( ContrabandIconGrid[k][n]:IsValid() ) then
				ContrabandIconGrid[k][n]:Remove();
				ContrabandIconGrid[k][n] = nil;
			end
			
		end
	
	end
	
	ContrabandIconGrid = nil;
	ContrabandIconGrid = { }
	
	ChosenContrabandItem = nil;

end

function RemoveContrabandButtons()

	if( not ContrabandMenu ) then return; end

	if( ContrabandMenu.TakeButton ) then
		ContrabandMenu.TakeButton:Remove();
		ContrabandMenu.TakeButton = nil;
	end

end

function ShowContrabandButtons()

	local function take()
	
		local canafford = false;
		
		if( ClientVars["Money"] - TS.ItemsData[ChosenContrabandItem.id].Price > 0 ) then
			
			RunConsoleCommand( "eng_buycbitem", ChosenContrabandItem.id, ChosenContrabandItem.x, ChosenContrabandItem.y );
			
			canafford = true;
			
		end
		
		if( not canafford ) then
		
			CreateOkPanel( "Cannot afford", "You do not have enough credits for this purchase" );
		
		end
	
	end

	ContrabandMenu.TakeButton = ContrabandMenu:AddButton( "Purchase", 10, 10, take );

end

function CreateContrabandMenu( msg )

	if( ContrabandMenu ) then
		ContrabandMenu:Remove();
		ContrabandMenu = nil;
	end
	
	RemoveAllContrabandIcons();
	RemoveContrabandButtons();
	
	local title = "Contraband"
	StorageW = 14
	StorageH = 8

	ContrabandMenu = CreateBPanel( title, 10, 10, 720, 510 );
	ContrabandMenu:SetBodyColor( Color( 20, 20, 20, 100 ) );
	
	ContrabandMenu.OnClose = function()
	
		RunConsoleCommand( "eng_closecb", "" );
	
	end

	ContrabandMenu.CanPutInto = false;
	
	ContrabandMenu.DescriptionPanel = CreateBPanel( nil, 150, 0, 560, 70 );
	ContrabandMenu.DescriptionPanel:SetParent( ContrabandMenu );
	ContrabandMenu.DescriptionPanel:SetBodyColor( Color( 20, 20, 20, 100 ) );
	
	ContrabandMenu.ContentPanel = CreateBPanel( nil, 10, 80, 700, 400 );	
	ContrabandMenu.ContentPanel:SetParent( ContrabandMenu );
	ContrabandMenu.ContentPanel:SetBodyColor( Color( 20, 20, 20, 100 ) );
	ContrabandMenu.ContentPanel:EnableScrolling( true );
	
	ContrabandMenu.ContentPanel.PaintHook = function()
	
		local row = StorageH - 1;
		local col = StorageW - 1;
		
		local black = false;
		local newblack = true;
		
		for y = 0, row do
		
			for x = 0, col do
			
				if( black ) then
					draw.RoundedBox( 0, x * GRIDWIDTH, y * GRIDHEIGHT, GRIDWIDTH, GRIDHEIGHT, Color( 0, 0, 0, 200 ) );
				end
				
				black = !black;
				
			end
			
			black = newblack;
			newblack = !newblack;
		
		end
	
		if( ChosenContrabandItem and ChosenContrabandItem.x > -1 ) then
		
			draw.RoundedBox( 0, ChosenContrabandItem.x * GRIDWIDTH, ChosenContrabandItem.y * GRIDHEIGHT, GRIDWIDTH * TS.ItemsData[ChosenContrabandItem.id].Width, GRIDHEIGHT * TS.ItemsData[ChosenContrabandItem.id].Height, Color( 0, 0, 128, 128 ) );
		
		end
	
	end
	
	local function ws()
	
			LocalPlayer():ConCommand( "eng_openws" );
			LocalPlayer():ConCommand( "eng_closecb" );

	end
	
	local function ln()
	
		LocalPlayer():ConCommand( "eng_olm" );
		
	end	
	ContrabandMenu.LoanButton = ContrabandMenu:AddButton( "Loans", 10, 41, ln );
	
	ContrabandMenu.WSButton = ContrabandMenu:AddButton( "Wholesale", 10, 60, ws );
	
	gui.EnableScreenClicker( true );
end
usermessage.Hook( "CCBM", CreateContrabandMenu );

function InsertContrabandItem( msg )

	if( not ContrabandMenu ) then return; end

	local item = msg:ReadString();
	local x = msg:ReadShort();
	local y = msg:ReadShort();

	local data = TS.ItemsData[item];
	
	if( not data ) then timer.Simple( 1, InsertContrabandItem, msg ); return; end
	
	local icon = vgui.Create( "DModelPanel", ContrabandMenu.ContentPanel );
	icon:SetModel( data.Model );
	icon:SetPos( x * GRIDWIDTH, y * GRIDHEIGHT );
	icon:SetSize( data.Width * GRIDWIDTH, data.Height * GRIDHEIGHT );
	icon:SetCamPos( data.CamPos );
	icon:SetLookAt( data.LookAt );
	icon:SetFOV( data.Fov );
	
	icon.LayoutEntity = function( self )
	    

 	end
 	
 	icon.OnMousePressed = function()
 	
 		ChosenContrabandItem = { id = item, x = x, y = y };
 		ShowContrabandButtons();
		ShowContrabandDescription( item );
 	
 	end
 	
 	if( ContrabandIconGrid[x] and ContrabandIconGrid[x][y] ) then

 		if( ContrabandIconGrid[x][y]:IsValid() ) then
 			ContrabandIconGrid[x][y]:Remove();
 		end
 		
 		ContrabandIconGrid[x][y] = nil;
 		
 	end
 	
 	if( not ContrabandIconGrid[x] ) then
 		ContrabandIconGrid[x] = { }
 	end
 	
 	ContrabandIconGrid[x][y] = icon;


end
usermessage.Hook( "ICI", InsertContrabandItem );

function RemoveContrabandItem( msg )

	local x = msg:ReadShort();
	local y = msg:ReadShort();

	if( ChosenContrabandItem and
		ChosenContrabandItem.x == x and
		ChosenContrabandItem.y == y ) then
		
		ChosenContrabandItem = nil;
		RemoveContrabandButtons();
		
	end
	
	if( ContrabandMenu.DescriptionPanel.Icon ) then
		ContrabandMenu.DescriptionPanel.Icon:Remove();
		ContrabandMenu.DescriptionPanel.Icon = nil;
	end

	if( ContrabandIconGrid[x] and ContrabandIconGrid[x][y] ) then
	
		ContrabandIconGrid[x][y]:Remove();
		ContrabandIconGrid[x][y] = nil;
		
	end
	

end
usermessage.Hook( "RCI", RemoveContrabandItem );

function ShowContrabandDescription( item )

	if( ContrabandMenu.DescriptionPanel.Icon ) then
		ContrabandMenu.DescriptionPanel.Icon:Remove();
		ContrabandMenu.DescriptionPanel.Icon = nil;
	end
	
	local data = TS.ItemsData[item];
	
	ContrabandMenu.DescriptionPanel.Icon = vgui.Create( "DModelPanel", ContrabandMenu.DescriptionPanel );
	ContrabandMenu.DescriptionPanel.Icon:SetModel( data.ContainsModel or data.Model );
	ContrabandMenu.DescriptionPanel.Icon:SetPos( 2, 2 );
	ContrabandMenu.DescriptionPanel.Icon:SetSize( math.Clamp( data.Width * GRIDWIDTH * 2, 0, ContrabandMenu.DescriptionPanel:GetWide() ), math.Clamp( data.Height * GRIDHEIGHT * 2, 0, ContrabandMenu.DescriptionPanel:GetTall() ) );
	ContrabandMenu.DescriptionPanel.Icon:SetCamPos( data.CamPos );
	ContrabandMenu.DescriptionPanel.Icon:SetLookAt( data.LookAt );
	ContrabandMenu.DescriptionPanel.Icon:SetFOV( data.Fov );
	
	ContrabandMenu.DescriptionPanel.Text = FormatLine( data.Desc, "NewChatFont", ContrabandMenu.DescriptionPanel:GetWide() - math.Clamp( data.Width * GRIDWIDTH * 2, 0, ContrabandMenu.DescriptionPanel:GetWide() ) );

	ContrabandMenu.DescriptionPanel.PaintHook = function()
	
		draw.DrawText( data.Name, "NewChatFont", ContrabandMenu.DescriptionPanel:GetWide() - 5, 2, Color( 255, 255, 255, 255 ), 2 );
		draw.DrawText( data.Price .. " credits", "NewChatFont", ContrabandMenu.DescriptionPanel:GetWide() - 5, 18, Color( 255, 255, 255, 255 ), 2 );
		draw.DrawText( ContrabandMenu.DescriptionPanel.Text, "NewChatFont", ContrabandMenu.DescriptionPanel:GetWide() - 2, 34, Color( 255, 255, 255, 255 ), 2 );
		
	end
	
	ContrabandMenu.DescriptionPanel.Icon.LayoutEntity = function( self ) 
	
	end
	
end

function RemoveContrabandMenu()
	
	if( ContrabandMenu and ContrabandMenu.TitleBar ) then
	
		ContrabandMenu.TitleBar:Remove();
		ContrabandMenu:Remove();

	end

end
usermessage.Hook( "RCBM", RemoveContrabandMenu );

